<?php
class FilesController{
	private $prefix;
	private $pathFiles;
	private $ext;
	const maxFileWeight = 10485760; // 1MB = 1048576;
	
	
	function __construct($path_,$prefix_=null/*prefix_width_height_filename.ext*/){
		$this->prefix = $prefix_;
		$this->pathFiles = $path_;
		$this->ext = array('jpg'=>'jpg','jpeg'=>'jpeg','gif'=>'gif','png'=>'png','zip'=>'zip','rar'=>'rar','bz2'=>'bz2','tar'=>'tar','doc'=>'doc','xls'=>'xls','pdf'=>'pdf','txt'=>'txt','ods'=>'ods','odt'=>'odt','pps'=>'pps','ppt'=>'ppt','flv'=>'flv','swf'=>'swf','avi'=>'avi','mov'=>'mov','mp3'=>'mp3');
	}
	
	public function saveFiles(){
		global $config;
		tools::_add_folder($this->pathFiles);
		foreach ($_FILES as $fieldName => $file) {

			if ($file['size'] <= FilesController::maxFileWeight && $file['error'] == 0 && isset($this->ext[tools::__rozszrzenie_pliku($file['name'])])){	
				// usuwam thumbs dla nowego zdjecia
				$name_ = tools::__link($file['name']);
				$file__ = "{$this->prefix}{$config->width}_{$config->height}_{$name_}";
				$files = tools::get_folders_files($this->pathFiles);
				foreach ($files[0]['files'] as  $path_ ) {
					$file_name = str_replace($this->pathFiles,'',$path_['nazwa']);
					$file_ = explode('.',$file_name);
					if ($file_[0] == $file__){
						tools::_delete_file($this->pathFiles."{$file_name}");
					}
				}									
				$up=move_uploaded_file($file['tmp_name'],  "{$this->pathFiles}{$file__}" );
			}else{
				$up = false;
			}				
		}
	}
	
	public function getFiles(){
		$files = tools::get_folders_files($this->pathFiles);
		$files_foto = array();
		$files_archiw = array();
		$files_doc = array();
		$files_video = array();
		$files_audio = array();
		$f = 0;
		$a = 0;
		$d = 0;
		$v = 0;
		$m = 0;
		foreach ($files[0]['files'] as  $path_ ) {
			$file_name = str_replace($this->pathFiles,'',$path_['nazwa']);
			$file_ = explode('.',$file_name);
			$x = count($file_);
			$ext = ($x == 2 ) ? 1 : $x - 1;
			$temp_ = explode('_',$file_[0]);
			$prefix_ = $temp_[0].'_'.$temp_[1].'_';
			$width_ = $temp_[2];
			$height_ = $temp_[3];
			if ($this->prefix == $prefix_ && $file_[$ext+1] != 'thumb'){
				switch(true){
					
					case ($file_[$ext]=='jpeg' || $file_[$ext]=='jpg' || $file_[$ext]=='gif' || $file_[$ext]=='png') :
						$files_foto[$f] = array('name'=>$file_name,'path'=>$this->pathFiles,'width'=>$width_,'height'=>$height_,'typ'=>$file_[$ext]);
						$f++;
						break;
					case $file_[$ext]=='zip' || $file_[$ext]=='rar' || $file_[$ext]=='bz2' || $file_[$ext]=='tar':
						$files_archiw[$a] = array('name'=>$file_name,'path'=>$this->pathFiles,'width'=>$width_,'height'=>$height_,'typ'=>$file_[$ext]);
						$a++;
						break;
					case $file_[$ext]=='doc' || $file_[$ext]=='xls' || $file_[$ext]=='pdf' || $file_[$ext]=='txt' || $file_[$ext]=='ods' || $file_[$ext]=='odt' || $file_[$ext]=='pps' || $file_[$ext]=='ppt':
						$files_doc[$d] = array('name'=>$file_name,'path'=>$this->pathFiles,'width'=>$width_,'height'=>$height_,'typ'=>$file_[$ext]);
						$d++;
						break;
					case $file_[$ext]=='flv' || $file_[$ext]=='swf' || $file_[$ext]=='mov' || $file_[$ext]=='avi':
						$files_video[$v] = array('name'=>$file_name,'path'=>$this->pathFiles,'width'=>$width_,'height'=>$height_,'typ'=>$file_[$ext]);
						$v++;
						break;		
					case $file_[$ext]=='mp3':
						$files_audio[$m] = array('name'=>$file_name,'path'=>$this->pathFiles,'width'=>$width_,'height'=>$height_,'typ'=>$file_[$ext]);
						$m++;
						break;				
				}
				
			}
			
		}
		return 	array('photos'=> $files_foto, 'archiw'=>$files_archiw, 'doc'=>$files_doc, 'video'=>$files_video, 'audio'=>$files_audio ) ;	
	}
	
	public function deleteFileAction(){
		global $objSession,$config;
		if ( !$objSession->IsLoggedIn()  && $objSession->GetUserData("stanowisko") == 'a'){	tools::_redirect('zaloguj_sie.html'); return;}
		$files = tools::get_folders_files($config->path);
		foreach ($files[0]['files'] as  $path_ ) {
			$file_name = str_replace($config->path,'',$path_['nazwa']);
			$file_ = explode('.',$file_name);
			$x = count($file_)-2;
			$thumb='';
			$org ='';

			for( $i=0; $i<$x; $i++ ){ 
				$thumb .= ($i==0)?$file_[$i]:'.'.$file_[$i]; 
			}
			
			$file__ = explode('.',$config->name);
			$x = count($file__)-1;

			for( $i=0;  $i<$x; $i++ ){ 
				$org .= ($i==0)?$file__[$i]:'.'.$file__[$i]; 
			}

			if ($thumb == $org){
				tools::_delete_file($config->path."{$file_name}");
			}
		}
		tools::_delete_file($config->path.$config->name);
		//tools::_redirect("sekcja,{$config->strona},{$config->sekcja},{$objSession->userLanguage}.html",null,0,'php');
		tools::_redirect($config->redirectTo,null,0,'php');
	}
}